//
//  QQSdkCallManager.m
//  Pods
//
//  Created by lu on 9/11/2019.
//

#import "QQSdkCallManager.h"
#import "FlutterQQResponseHandler.h"

@implementation QQSdkCallManager

+ (QQSdkCallManager *)getInstance {
    static QQSdkCallManager *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (instance == nil) {
            instance = [[QQSdkCallManager alloc] init];
        }
    });
    return instance;
}

// 初始化QQ SDK
- (Boolean)initQQWithAppId:(NSString *)_appId universalLinks:(NSString *)_universalLinks {
    self.tententOAuth = [[TencentOAuth alloc] initWithAppId:_appId andUniversalLink:_universalLinks andDelegate:self];
    self.qqUniversalLink = _universalLinks;
    NSLog(@"self.tententOAuth = 1: %@", self.tententOAuth);
    if (self.tententOAuth != nil) {
        return YES;
    }
    return NO;
}

// MARK: - TencentSessionDelegate

// =========  登录回调

// 登录成功后回调
- (void)tencentDidLogin {
    NSLog(@"登录成功后回调");
    if (self.tententOAuth.accessToken && 0 != [self.tententOAuth.accessToken length]) {
        // 记录登录用户的OpenID、Token以及过期时间
        NSLog(@"获取到的accessToken: %@", self.tententOAuth.accessToken);
        NSLog(@"获取到的OpenID: %@", self.tententOAuth.openId);
        NSLog(@"获取到的unionid: %@", self.tententOAuth.unionid);
        [self.tententOAuth RequestUnionId];
        [self.tententOAuth getUserInfo];
        NSLog(@"qq登陆 %@", self.tententOAuth.accessToken);
        
        
        
    } else {
        NSLog(@"登录不成功 没有获取accesstoken");
        
        NSDictionary *result = @{
                @"errCode": @(1),
                @"platform": @"iOS"
        };
        
        // 处理qq登录的回调
        [[FlutterQQResponseHandler defaultManager].fluQQMethodChannel invokeMethod:@"onQQAuthResponse" arguments:result];
    }
}

- (void)didGetUnionID {
    NSLog(@"获取到的unionid======: %@", self.tententOAuth.unionid);

    NSDictionary *result = @{
            @"accessToken": self.tententOAuth.accessToken == nil ? @"" : self.tententOAuth.accessToken,
            @"openID": self.tententOAuth.openId == nil ? @"" : self.tententOAuth.openId,
            @"unionid": self.tententOAuth.unionid == nil ? @"" : self.tententOAuth.unionid,
            @"errCode": @(0),
            @"platform": @"iOS"
    };
    
    // 处理qq登录的回调
    [[FlutterQQResponseHandler defaultManager].fluQQMethodChannel invokeMethod:@"onQQAuthResponse" arguments:result];
}

// 登录失败后回调
- (void)tencentDidNotLogin:(BOOL)cancelled {
    NSLog(@"登录失败后回调");
}

// 登录时有网络问题的回调
- (void)tencentDidNotNetWork {
    NSLog(@"登录时有网络问题的回调");
}

// =========  获取用户信息回调

// 获取用户信息回调
- (void)getUserInfoResponse:(APIResponse *)response {
    if (URLREQUEST_SUCCEED == response.retCode && kOpenSDKErrorSuccess == response.detailRetCode) {
        NSMutableString *str = [NSMutableString stringWithFormat:@""];
        for (id key in response.jsonResponse) {
            [str appendString: [NSString stringWithFormat: @"%@:%@\n", key, [response.jsonResponse objectForKey:key]]];
        }
        
        NSLog(@"获取到的用户信息数据： %@", [NSString stringWithFormat:@"%@",str]);
        
        NSDictionary *result = @{
                @"avatarUrl": [response.jsonResponse objectForKey:@"figureurl_qq"] == nil ? @"" : [response.jsonResponse objectForKey:@"figureurl"],
                @"province": [response.jsonResponse objectForKey:@"province"] == nil ? @"" : [response.jsonResponse objectForKey:@"province"],
                @"city": [response.jsonResponse objectForKey:@"city"] == nil ? @"" : [response.jsonResponse objectForKey:@"city"],
                @"nickname": [response.jsonResponse objectForKey:@"nickname"] == nil ? @"" : @"",
                @"gender": [response.jsonResponse objectForKey:@"gender"] == nil ? @"" : [response.jsonResponse objectForKey:@"gender"],
                @"genderType": [[response.jsonResponse objectForKey:@"gender_type"] integerValue] == 0 ? @(2) : @([[response.jsonResponse objectForKey:@"gender_type"] integerValue]),
                @"year": [[response.jsonResponse objectForKey:@"year"] integerValue] == 0 ? @(1990) : @([[response.jsonResponse objectForKey:@"year"] integerValue]),
                @"errCode": @(0),
                
        };
        
        NSLog(@"result结果：%@", result);
        
        if ([FlutterQQResponseHandler defaultManager].fluQQMethodChannel == nil) {
            NSLog(@"11111 - shibai");
        }
        
        // 处理qq用户信息的回调
        [[FlutterQQResponseHandler defaultManager].fluQQMethodChannel invokeMethod:@"onQQGetUserInfoResponse" arguments:result];
        
    } else {
        NSString *errMsg = [NSString stringWithFormat:@"errorMsg:%@\n%@", response.errorMsg, [response.jsonResponse objectForKey:@"msg"]];
        NSLog(@"获取到的用户信息失败： %@", [NSString stringWithFormat:@"%@",errMsg]);
        
        NSDictionary *result = @{
                @"errCode": @(1),
        };
        
        // 处理qq登录的回调
        [[FlutterQQResponseHandler defaultManager].fluQQMethodChannel invokeMethod:@"onQQGetUserInfoResponse" arguments:result];
    }
    
    
    // 获取到的用户信息数据
//    figureurl:http://qzapp.qlogo.cn/qzapp/101815213/6BC3211A15D76B8DB01D0FABED46B744/30
//    province:北京
//    nickname:🥳营🍼
//    figureurl_qq_1:http://thirdqq.qlogo.cn/g?b=oidb&k=eXMJjlKiaGfrHCwTnSf7N2Q&s=40&t=1573532992
//    constellation:
//    yellow_vip_level:0
//    city:通州
//    year:1988
//    figureurl_1:http://qzapp.qlogo.cn/qzapp/101815213/6BC3211A15D76B8DB01D0FABED46B744/50
//    figureurl_2:http://qzapp.qlogo.cn/qzapp/101815213/6BC3211A15D76B8DB01D0FABED46B744/100
//    gender:男
//    gender_type:1
//    level:0
//    is_yellow_year_vip:0
//    figureurl_type:1
//    is_lost:0
//    ret:0
//    figureurl_qq_2:http://thirdqq.qlogo.cn/g?b=oidb&k=eXMJjlKiaGfrHCwTnSf7N2Q&s=100&t=1573532992
//    vip:0
//    figureurl_qq:http://thirdqq.qlogo.cn/g?b=oidb&k=eXMJjlKiaGfrHCwTnSf7N2Q&s=140&t=1573532992
//    msg:
//    is_yellow_vip:0
}

@end
